<div id="app"></div>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
  // 定义组件
  class App extends React.Component {
    state = {
      num: 0,
    };

    render() {
      return (
        <fieldset>
          <legend>setState同步或异步</legend>
          <h1>num {this.state.num}</h1>

          <button
            onClick={() => {
              console.log("更新前：", this.state.num);
              this.setState({ num: this.state.num + 1 });
              console.log("更新后：", this.state.num);
            }}
          >
            +1
          </button>
        </fieldset>
      );
    }

    // 组件挂载完毕
    // vue mounted  onMounted
    // react componentDidMount
    componentDidMount() {
      //   // this组件对象
      //   console.log("更新前：", this.state.num);
      //   this.setState({ num: this.state.num + 1 });
      //   console.log("更新后：", this.state.num);
      //   document.onclick = () => {
      //     console.log("更新前：", this.state.num);
      //     this.setState({ num: this.state.num + 1 });
      //     console.log("更新后：", this.state.num);
      //   };
      setTimeout(() => {
        console.log("更新前：", this.state.num);
        this.setState({ num: this.state.num + 1 });
        console.log("更新后：", this.state.num);
      });
    }

    // 点击事件中：分页+1
    // this.setState({ pagenum: this.state.pagenum + 1 });
    // fetch请求接口获取最新页数据  bug？
    // 问题：fetch执行的时候拿到的不是最新更新的数据
    // 解决：在setState第二个实参callback中去请求接口
  }
  // 渲染
  ReactDOM.render(<App />, document.querySelector("#app"));
</script>
